//
//  QSNetworkTool.m
//  SinaWeiBo
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSNetworkTool.h"

@implementation QSNetworkTool
/**
 *  创建单例
 */
+ (instancetype)shareNetworkInstance
{
    // 初始化单例对象
    static QSNetworkTool *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[QSNetworkTool alloc] init];
        instance.afManager = [[AFHTTPSessionManager alloc] init];
        instance.afManager.requestSerializer.timeoutInterval = 15;
    });
    
    return instance;
}

/**
 *  post
 */
- (void)requestWebWithParaWithURL:(NSString *)url Parameter:(NSDictionary *)param Finish:(HttpResponseSucBlock)completeBlock Error:(HttpResponseErrBlock)errorBlock
{
    // 有时可能有抽取一个公共的url前缀，所以要进行拼接
//    url =  [BaseURL stringByAppendingString:url];
    
    [self.afManager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completeBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(task, error, @"");
    }];
}

/**
 *  get
 */
- (void)getWithURL:(NSString *)url Parameter:(NSDictionary *)param Finish:(HttpResponseSucBlock)completeBlock Error:(HttpResponseErrBlock)errorBlock
{
    [self.afManager GET:url parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completeBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(task, error, @"");
    }];
}

/**
 *  上传文件
 */
- (void)updloadDataWithUrl:(NSString *)url Parameter:(NSDictionary *)param updata:(HttpUploadDataBlock)uploadDataBlock Finish:(HttpResponseSucBlock)completeBlock Error:(HttpResponseErrBlock)errorBlock
{
    [self.afManager POST:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        uploadDataBlock(formData);
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completeBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(task, error, @"");
    }];
}

/**
 *  重写networkStatus的get方法
 */
- (NSInteger)networkStatus
{
    NetworkStatus netStatus;
    
    // 创建NetworkReachabilityManager管理者
    AFNetworkReachabilityManager *netReManager = [AFNetworkReachabilityManager sharedManager];
    
    // 取出连接状态
    AFNetworkReachabilityStatus netReStatus = netReManager.networkReachabilityStatus;
    
    // 判断
    /*AFNetworkReachabilityStatusUnknown          = -1,
     AFNetworkReachabilityStatusNotReachable     = 0,
     AFNetworkReachabilityStatusReachableViaWWAN = 1,
     AFNetworkReachabilityStatusReachableViaWiFi = 2,*/
    switch (netReStatus) {
        case AFNetworkReachabilityStatusUnknown:
        case AFNetworkReachabilityStatusNotReachable:
            netStatus = networkStatusNone;
            break;
            
        case AFNetworkReachabilityStatusReachableViaWWAN:
            netStatus = networkStatusPhone;
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            netStatus = networkStatusWifi;
            break;
    }
    
    return netStatus;
}
@end
